package com.zcn.io.web.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zcn.io.common.vo.OrderMainForTrusteeshipVo;
import com.zcn.io.common.vo.OrderMainPayDetailVo;
import com.zcn.io.web.api.query.OrderMainQuery;
import com.zcn.io.web.api.vo.OrderMainVo;
import com.zcn.io.web.api.vo.PhoneCardVo;
import com.zcn.io.web.excel.OrderListener;
import com.zcn.io.web.service.OrderService;
import com.zcn.io.common.entity.OrderMain;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.zcn.io.common.pojo.CommonResult;

import java.io.IOException;
import java.util.List;

/**
 * @desc:
 * @author: 202438 邱建伟
 * @date: 2024/9/22 10:38
 * @Copyright (c) 2024, 江苏金恒信息科技股份有限公司
 */
@RestController
@AllArgsConstructor
@RequestMapping("/order")
public class OrderController {

    private final OrderService orderService;


    @GetMapping("/list")
    @PreAuthorize("hasAuthority('super_man')")
    public CommonResult<Page<OrderMain>> list(Page<OrderMain> page, OrderMainQuery orderMain) {
        orderService.list(page, orderMain);
        return CommonResult.success(page);
    }

    @PostMapping("/save")
    public CommonResult<OrderMain> save(@RequestBody OrderMain orderMain) {
        orderService.save(orderMain);
        return CommonResult.success(orderMain);
    }

    @GetMapping("/getById")
    public CommonResult<OrderMain> getById(@RequestParam String id) {
        return CommonResult.success(orderService.getById(id));
    }

    @PostMapping("/updateById")
    public CommonResult<OrderMain> updateById(@RequestBody OrderMain orderMain) {
        orderService.updateById(orderMain);
        return CommonResult.success(orderMain);
    }

    @DeleteMapping("/deleteById")
    public CommonResult<String> deleteById(@RequestBody OrderMain orderMain) {
        orderService.deleteById(orderMain.getId());
        return CommonResult.success(orderMain.getId());
    }

    @PostMapping("/importData")
    public CommonResult<String> importData(@RequestParam("file") MultipartFile file) {
        OrderListener orderListener = new OrderListener(orderService);
        try {
            ExcelReaderBuilder read = EasyExcel.read(file.getInputStream(), orderListener);
            read.sheet().doRead();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return CommonResult.success("成功");
    }

    @RequestMapping("/setOrderTotalPay")
    public CommonResult<String> setOrderTotalPay(@RequestBody OrderMain orderMain) {
        orderService.setOrderTotalPay(orderMain);
        return CommonResult.success("成功");
    }

    @GetMapping("/getDetailById")
    @PreAuthorize("hasAuthority('super_man')")
    public CommonResult<OrderMainPayDetailVo> getDetailById(@RequestParam String orderId) {
        return CommonResult.success(orderService.getDetailById(orderId));
    }

    @GetMapping("/listOrderInfoForTrusteeship")
    public CommonResult<Page<OrderMainForTrusteeshipVo>> listOrderInfoForTrusteeship(Page<OrderMainForTrusteeshipVo> page, OrderMainQuery orderMain) {
        orderService.listOrderInfoForTrusteeship(page, orderMain);
        return CommonResult.success(page);
    }

    @PostMapping("/updateNativePlaceHandle")
    public CommonResult<String> updateNativePlaceHandle(@RequestBody OrderMain orderMain) {
        orderService.updateNativePlaceHandle(orderMain);
        return CommonResult.success("编辑成功");
    }





    //获取欠费
    @GetMapping("/getReminderTimeList")
    public CommonResult<Page<OrderMainVo>> getReminderTimeList(Page<OrderMainVo> page, OrderMainQuery orderMain) {
        orderService.getReminderTimeList(page, orderMain);
        return CommonResult.success(page);
    }

    @PostMapping("/updateReminderMemo")
    public CommonResult<String> updateReminderMemo(@RequestBody OrderMain orderMain) {
        orderService.updateReminderMemo(orderMain);
        return CommonResult.success("保存成功");
    }


    @PostMapping("/saveCreditCardInfo")
    public CommonResult<String> saveCreditCardInfo(@RequestBody OrderMain orderMain) {
        orderService.saveCreditCardInfo(orderMain);
        return CommonResult.success("保存成功");
    }

    @PostMapping("/updateOnlineLendingInfo")
    public CommonResult<String> updateOnlineLendingInfo(@RequestBody OrderMain orderMain) {
        orderService.updateOnlineLendingInfo(orderMain);
        return CommonResult.success("保存成功");
    }

    @GetMapping("/getUseOrderList")
    public CommonResult<List<OrderMainPayDetailVo>> getUseOrderList(OrderMainQuery orderMain) {
        return CommonResult.success(orderService.getUseOrderList(orderMain));
    }


    //获取无用的订单信息
    @GetMapping("/getInvalidOrderMain")
    public CommonResult<Page<OrderMain>> getInvalidOrderMain(Page<OrderMain> page, OrderMainQuery orderMain) {
        orderService.getInvalidOrderMain(page, orderMain);
        return CommonResult.success(page);
    }

    /**
    * @desc: 设置退单备注
    * @author 202438 邱建伟
    * @date 2025/5/4 20:17
    */
    @PostMapping("/setCancelMemo")
    public CommonResult<String> setCancelMemo(@RequestBody OrderMain orderMain) {
        orderService.setCancelMemo(orderMain);
        return CommonResult.success("保存成功");
    }
}
